#include "mainwindow.h"
#include "ghost.h"
#include "player.h"

/*

  GAME SCENE CLASS

  */

GameScene::GameScene(int x, int y, int w, int h)
    :QGraphicsScene(x, y, w, h)
{

}

void GameScene::drawBackground(QPainter *painter, const QRectF &rect){
    QPixmap bgPix(":/common/images/0026.jpg");
    painter->drawPixmap(-bgPix.width()/2,-bgPix.height()/2, bgPix);
}

/*

  MAIN VIEW CLASS

  */

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createScene();
    createView();

    //window config
    setMaximumSize(SCENE_WIDTH, SCENE_HEIGHT);
    setMinimumSize(SCENE_WIDTH, SCENE_HEIGHT);
    setCentralWidget(view);
    setWindowTitle("Ghost Story");

}

MainWindow::~MainWindow()
{
    delete controller;
    delete table;
    delete game;
    delete scene;
    delete view;
}


void MainWindow::createScene(){

    scene = new GameScene(-SCENE_WIDTH/2, -SCENE_HEIGHT/2, SCENE_WIDTH, SCENE_HEIGHT);
    game = new QGraphicsRectItem;
    table = new PlayerTable(game);
    controller = new GamePhaseController(game, table);
    scene->addItem(game);

    GhostInfo *ggi = new GhostInfo;
    scene->addItem( ggi );
    ggi->setPos( -( SCENE_WIDTH - ggi->width() )/2 , -( SCENE_HEIGHT - ggi->height() )/2 + 10 );
    ggi->setZValue( ggi->zValue() - 1 );

    GhostDeck *deck = new GhostDeck;
    deck->setGhostInfo( ggi );
    deck->setPlayerTable( table );
    scene->addItem( deck );
    deck->setPos( ( SCENE_WIDTH - deck->width() )/ 2, -( SCENE_HEIGHT - deck->height() )/2 + 10 );
    deck->setZValue( deck->zValue() - 1 );

    deck->setGhostInfo( ggi );

    InputBlocker *ib = new InputBlocker;
    GhostCutIn *gutin = new GhostCutIn( 0, deck, ib );
    scene->addItem( ib );
    scene->addItem( gutin );

    connect( table, SIGNAL(hoveredGhost(Ghost*)), ggi, SLOT(setGhost(Ghost*)) );

    //add player information
    /*
    PlayerInfo *redpi = new PlayerInfo(player::RED);
    QGraphicsProxyWidget *redproxy = scene->addWidget(redpi);
    redproxy->setPos( SCENE_WIDTH/2-210, -SCENE_HEIGHT/2+10 );

    PlayerInfo *bluepi = new PlayerInfo(player::BLUE);
    QGraphicsProxyWidget *blueproxy = scene->addWidget(bluepi);
    blueproxy->setPos( SCENE_WIDTH/2-110, -SCENE_HEIGHT/2+10 );

    PlayerInfo *yellowpi = new PlayerInfo(player::YELLOW);
    QGraphicsProxyWidget *yellowproxy = scene->addWidget(yellowpi);
    yellowproxy->setPos( SCENE_WIDTH/2-210, -SCENE_HEIGHT/2+300 );

    PlayerInfo *greenpi = new PlayerInfo(player::GREEN);
    QGraphicsProxyWidget *greenproxy = scene->addWidget(greenpi);
    greenproxy->setPos( SCENE_WIDTH/2-110, -SCENE_HEIGHT/2+300 );
    */
}

void MainWindow::createView(){

    view = new QGraphicsView(scene);
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setCacheMode(QGraphicsView::CacheBackground);
    view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    view->resize(SCENE_WIDTH, SCENE_HEIGHT);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
